package com.erp.framework.types;

import java.math.BigDecimal;

import com.erp.framework.SimpleConstants;

public class DataGuard implements SimpleConstants{

    static public BigDecimal getBigDecimal(Object o, int iScale) {

        return getBigDecimal(o).setScale(iScale, BigDecimal.ROUND_UP);
    }

    static public BigDecimal getBigDecimal(Object o) {

        BigDecimal bi = new BigDecimal("0");
        if (null != o) {
            if (o instanceof Number) {
                bi = new BigDecimal(new Double(((Number) o).doubleValue()).toString());
            } else if (o instanceof String) {
                try {
                    bi = new BigDecimal((String) o);
                } catch (Throwable e) {
                }
            }
        }
        return bi;
    }

    static public int getInt(Object o) {

        return getBigDecimal(o).intValue();
    }

    static public Integer getInteger(Object o) {

        if (o instanceof Integer)
            return (Integer) o;
        else
            return new Integer(getInt(o));
    }

    static public long getLong(Object o) {

        return getBigDecimal(o).longValue();
    }

    static public double getDouble(Object o) {

        return getBigDecimal(o).doubleValue();
    }

    static public String getSTRING(Object o) {

        if (null != o) {
            if (o instanceof String)
                return o.toString().toUpperCase();
            else
                return o.toString().toUpperCase();
        }
        return null;
    }

    static public String getString(Object o) {

        if (null != o)
            return o.toString();
        return "";
    }

    static public boolean isNothing(Object o) {

        if (null != o) {
            if (o instanceof String)
                return "".equals(((String) o).trim());
        } else
            return true;
        return false;
    }

    static public boolean exceedLength(String s, int len) {

        if (!isNothing(s)) {
            // Sybase database uses 3 bytes for storing a chinese character,
            // therefore, i*2 is adjusted
            // for it.
            int i = 0;
            try {
                return s.getBytes("utf-8").length > len;
            } catch (java.io.UnsupportedEncodingException e) {
                return true;
            }
            /*
             * try { i = s.getBytes("BIG5").length; } catch (
             * java.io.UnsupportedEncodingException e ) { i = s.length(); }
             * return s.length() + (i - s.length())*2 > len;
             */
        }
        return false;
    }

    static public boolean getBooleanValue(Object o) {

        if (!isNothing(o)) {
            Boolean b = (Boolean) o;
            return ((Boolean) o).booleanValue();
        }
        return false;
    }
}